package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Person;
import com.ericom.service.GenericManager;

public class PersonAction extends BaseAction {
	private GenericManager<Person, Long> personManager;
	private List persons;
	private Person person;
	private Long id;

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setPersonManager(GenericManager<Person, Long> personManager) {
		this.personManager = personManager;
	}

	public String list() {
		persons = personManager.getAll();
		return SUCCESS;
	}

	public List getPersons() {
		return persons;
	}

	public String delete() {
		personManager.remove(person.getId());
		this.saveMessage(getText("person.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			person = personManager.get(id);
		} else {
			person = new Person();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (person.getId() == null);
		person = personManager.save(person);

		String key = (isNew) ? "person.added" : "person.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}

}
